package com.mac.cxcybackground.controller;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mac.cxcybackground.bean.Activities;
import com.mac.cxcybackground.mapper.ActivitiesMapper;
import com.mac.cxcybackground.service.ActivitiesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

@Controller
public class ActivitiesController {
    @Autowired
    ActivitiesService activitiesService;
    @Autowired
    ActivitiesMapper activitiesMapper;
    @GetMapping("/activities_table")
    public String ActivitiesFind(@RequestParam(value = "pn",defaultValue = "1")Integer pn,
                                 Model model){

        Page<Activities> activitiesPage = new Page<>(pn, 8);
        Page<Activities> page = activitiesService.page(activitiesPage,null);
        model.addAttribute("page",page);
        return "table/activities_table";
    }

    @GetMapping("/activities/delete/{aId}")
    public String ActivitiesDelete(@PathVariable("aId") Long id,
                                   @RequestParam(value = "pn",defaultValue = "1") Integer pn,
                                   RedirectAttributes ra){
        activitiesService.removeById(id);
        ra.addAttribute("pn",pn);
        return "redirect:/activities_table";
    }

    @GetMapping("/activities_add_table")
    public String activities_add_table(){
        return "table/activities_add_table";
    }
    @PostMapping("/activities/add")
    public String ActivitiesAdd(Activities activities,Model model){
       Activities activity =  new Activities(null,activities.getAShow(),
               activities.getTitle(),activities.getAGrade(),activities.getATime(),
               activities.getCredit());
       activitiesService.save(activity);
       model.addAttribute("success","添加成功");
       return "redirect:/activities_table";

    }
    @RequestMapping("/activities/update-get/{aId}")
    public String ActivitiesUpdate_get(@PathVariable("aId") Long id,
                                   @RequestParam(value = "pn",defaultValue = "1") Integer pn,
                                   RedirectAttributes ra,Model model){
        Activities activity = activitiesService.getById(id);
        ra.addAttribute("pn",pn);
        model.addAttribute("activity",activity);
        return "table/activities_update_table";

    }
    @GetMapping("/activities/update/{aId}")
    public String ActivitiesUpdate(@PathVariable("aId") Long id,Activities activities){
        Activities activity =  new Activities(id,activities.getAShow(),
                activities.getTitle(),activities.getAGrade(),activities.getATime(),
                activities.getCredit());

        activitiesService.updateById(activity);
        return "redirect:/activities_table";
    }
}
